package com.yntx.usercenter.common.config.sharding;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * 承租者id提示切分算法
 *
 * @author jielihaofeng
 * @date 2023-04-29 02:15
 */
@Slf4j
public class TenantIdHintShardingAlgorithm implements HintShardingAlgorithm<String> {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> shardingValue) {
        log.info("#doSharding# shardingValue:{},availableTargetNames:{}", shardingValue, availableTargetNames);
        List<String> shardingResult = new ArrayList<>();
        for (String targetName : availableTargetNames) {
            for (String value : shardingValue.getValues()) {
                if (targetName.endsWith(value)) {
                    shardingResult.add(targetName);
                }
            }
        }
        log.info("#doSharding# shardingResult:{}", shardingResult);
        return shardingResult;
    }

    @Override
    public String getType() {
        return "CLASS_BASED";
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init() {

    }

}
